/*
 * Copyright (C) 冷锋
 */
#include "echosvr.h"
#include <stdlib.h>
#include <string.h>
CEchoSvr::CEchoSvr(char*sIP,const short iPort):
CServer(sIP,iPort)
{
	
}
int CEchoSvr::Handle( HandleBuffer*buf )
{
	
	Connection*pConn = buf->pConn;
	Packet*packet = &buf->packet;
	char*msg = new char[packet->len+1];
	msg[packet->len]=0;
	memcpy( msg,packet->body,packet->len );
	//printf("receive data:%s\n",msg);
	delete msg;
	//HandleBuffer*pSendBuf = new HandleBuffer;
	HandleBuffer*pSendBuf = (HandleBuffer*)malloc( sizeof(HandleBuffer)+packet->len );
	pSendBuf->iPos = 0;
	pSendBuf->iSize = sizeof(Packet)+packet->len;//包头+包体
	pSendBuf->lSeq = pConn->lSeq;
	pSendBuf->pConn = pConn;
	memcpy( &pSendBuf->packet,packet,sizeof(Packet)+packet->len );
	Send(pSendBuf);

	this->DelayCall(1000,NULL,NULL);
	this->DelayCall(2000,NULL,NULL);
	this->DelayCall(4000,NULL,NULL);
	this->DelayCall(5000,NULL,NULL);
	this->DelayCall(8000,NULL,NULL);

}
void ShowUsage(const char* proc)
{
	printf("Usage:%s -h <host> -p <port>\n",proc);
}

CEchoSvr* g_pEchoSvr = NULL;
int main( int argc,char**argv )
{
	char* sIP="192.168.137.222";
	unsigned short iPort = 8888;
	extern char* optarg;
	int c;
	SetStartTime();
	/*
	while ( (c= getopt(argc,argv,"h:p:")) )
	{
		//printf("args:%c\n",c);
		switch ( c )
		{
		case 'h'://host address
			sIP = optarg;
			printf("ip:%s\n",sIP);
			break;
		case 'p':
			iPort = strtoul( optarg,NULL,10 );
			printf("port:%u\n",iPort);
			break;
		default:
			printf("other arg:%c\n",c);
			//ShowUsage(argv[0]);
			//exit(0);
		}
	}*/
	if ( sIP == NULL||iPort == 0 )
	{
		exit(-1);
	}
	g_pEchoSvr = new CEchoSvr( sIP,iPort );
	g_pEchoSvr->Open();
	g_pEchoSvr->Loop();
	return 0;
}
